package org.dromara.system.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.TQuestionVo;
import org.dromara.system.domain.bo.TQuestionBo;
import org.dromara.system.service.ITQuestionService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 问题项
 *
 * @author Lion Li
 * @date 2024-10-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/question")
public class TQuestionController extends BaseController {

    private final ITQuestionService tQuestionService;

    /**
     * 查询问题项列表
     */
    @SaCheckPermission("system:question:list")
    @GetMapping("/list")
    public TableDataInfo<TQuestionVo> list(TQuestionBo bo, PageQuery pageQuery) {
        pageQuery.setOrderByColumn("order_index");
        pageQuery.setIsAsc("asc");
        return tQuestionService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出问题项列表
     */
    @SaCheckPermission("system:question:export")
    @Log(title = "问题项", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TQuestionBo bo, HttpServletResponse response) {
        List<TQuestionVo> list = tQuestionService.queryList(bo);
        ExcelUtil.exportExcel(list, "问题项", TQuestionVo.class, response);
    }

    /**
     * 获取问题项详细信息
     *
     * @param questionId 主键
     */
    @SaCheckPermission("system:question:query")
    @GetMapping("/{questionId}")
    public R<TQuestionVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Integer questionId) {
        return R.ok(tQuestionService.queryById(questionId));
    }

    /**
     * 新增问题项
     */
    @SaCheckPermission("system:question:add")
    @Log(title = "问题项", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TQuestionBo bo) {
        return toAjax(tQuestionService.insertByBo(bo));
    }

    /**
     * 修改问题项
     */
    @SaCheckPermission("system:question:edit")
    @Log(title = "问题项", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TQuestionBo bo) {
        return toAjax(tQuestionService.updateByBo(bo));
    }

    /**
     * 删除问题项
     *
     * @param questionIds 主键串
     */
    @SaCheckPermission("system:question:remove")
    @Log(title = "问题项", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Integer[] questionIds) {
        return toAjax(tQuestionService.deleteWithValidByIds(List.of(questionIds), true));
    }
}
